package belle.com.myapp.view.fm.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import belle.com.myapp.R;
import belle.com.myapp.base.BaseActivity;
import belle.com.myapp.manager.ToolBarManager;
import belle.com.myapp.retrofit.ApiService;
import belle.com.myapp.view.fm.adapter.EveryAdapter;
import belle.com.myapp.view.fm.model.EveryResponse;
import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @author guanjiali1
 */
public class EveryActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rv_list)
    RecyclerView rvList;

    private EveryResponse everyResponse = new EveryResponse();
    private EveryAdapter everyAdapter;

    @Override
    protected int isTranslucentStatus() {
        return 0;
    }

    @Override
    protected boolean isNeedEventBus() {
        return false;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_every;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        ToolBarManager.setToolBarBack(this, toolbar, "每日推荐", R.menu.menu_help_image, new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                return false;
            }
        });
        setRecycleView();
    }

    private void setRecycleView() {
        rvList.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        everyAdapter = new EveryAdapter(R.layout.include_music_layout, everyResponse.getRecommend());
        rvList.setAdapter(everyAdapter);
    }

    @Override
    protected void initData() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://10.20.30.216:3000/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ApiService service = retrofit.create(ApiService.class);
        Call<EveryResponse> call = service.songs(true);
        call.enqueue(new Callback<EveryResponse>() {
            @Override
            public void onResponse(@NonNull Call<EveryResponse> call, @NonNull Response<EveryResponse> response) {
                everyResponse = response.body();
                everyAdapter.setNewData(everyResponse.getRecommend());
//                everyAdapter.addData(everyResponse.getRecommend());
            }

            @Override
            public void onFailure(@NonNull Call<EveryResponse> call, @NonNull Throwable t) {

            }
        });
    }

    @Override
    protected void getBundleExtras(Bundle extras) {

    }

}
